#include "globalvar.h"
#include <QSettings>
#include <QDebug>

int globalvar::iCurlevel = NEW_L;
sudoku globalvar::m_sudoku = sudoku();
GameLogic globalvar::m_logic = GameLogic();
MainWindow* globalvar::m_mainwindow = NULL;
TimeItem*   globalvar::m_timeitem = NULL;
QString globalvar::m_strfresh = "";
QString globalvar::m_strnormal = "";
QString globalvar::m_strmaster = "";
QString globalvar::m_strgun = "";
bool globalvar::m_bVoice = false;
QTranslator* globalvar::m_trans = NULL;

globalvar::globalvar()
{
}

void globalvar::setName(const QString &name)
{
    QSettings settings("MySoft", "sudoku");
    settings.setValue("login/name",name);
}

QString globalvar::name()
{
    QSettings settings("MySoft", "sudoku");
    return settings.value("login/name", "").toString();
}

int globalvar::level()
{
    QSettings settings("MySoft", "sudoku");
    return settings.value("main/level",NEW_L).toInt();
}

void globalvar::setLevel(int l)
{
    QSettings settings("MySoft","sudoku");
    settings.setValue("main/level",l);
}

int globalvar::successNum()
{
    QSettings   setting("MySoft","sudoku");
    return  setting.value("game/success",0).toInt();
}

void globalvar::setSuccessNum(int n)
{
    QSettings   setting("MySoft","sudoku");
    setting.setValue("game/success",n);
}

MyTime globalvar::currentTime()
{
    MyTime mt;
    QSettings   setting("MySoft","sudoku");
    mt.ihour = setting.value("curtime/hour",0).toInt();
    mt.iminute = setting.value("curtime/minute",0).toInt();
    mt.isecond = setting.value("curtime/second",0).toInt();
    return mt;
}

void globalvar::setCurTime(const MyTime &mt)
{
    QSettings setting("MySoft","sudoku");
    setting.setValue("curtime/hour",mt.ihour);
    setting.setValue("curtime/minute",mt.iminute);
    setting.setValue("curtime/second",mt.isecond);
}

MyTime globalvar::levelTime(int l)
{
    MyTime mt;
    QSettings   setting("MySoft","sudoku");
    switch(l)
    {
    case NEW_L:
    {
        mt.ihour = setting.value("newlevel/hour",0).toInt();
        mt.iminute = setting.value("newlevel/minute",0).toInt();
        mt.isecond = setting.value("newlevel/second",0).toInt();
    }
        break;
    case NORMAL_L:
    {
        mt.ihour = setting.value("normallevel/hour",0).toInt();
        mt.iminute = setting.value("normallevel/minute",0).toInt();
        mt.isecond = setting.value("normallevel/second",0).toInt();
    }
        break;
    case MASTER_L:
    {
        mt.ihour = setting.value("masterlevel/hour",0).toInt();
        mt.iminute = setting.value("masterlevel/minute",0).toInt();
        mt.isecond = setting.value("masterlevel/second",0).toInt();
    }
        break;
    case GUN_L:
    {
        mt.ihour = setting.value("gunlevel/hour",0).toInt();
        mt.iminute = setting.value("gunlevel/minute",0).toInt();
        mt.isecond = setting.value("gunlevel/second",0).toInt();
    }
        break;
    default:
    {

    }
    }
    return mt;
}

void globalvar::setLevelTime(const MyTime &mt, int l)
{
    QSettings   setting("MySoft","sudoku");
    switch(l)
    {
    case NEW_L:
    {
        setting.setValue("newlevel/hour",mt.ihour);
        setting.setValue("newlevel/minute",mt.iminute);
        setting.setValue("newlevel/second",mt.isecond);
    }
        break;
    case NORMAL_L:
    {
        setting.setValue("normallevel/hour",mt.ihour);
        setting.setValue("normallevel/minute",mt.iminute);
        setting.setValue("normallevel/second",mt.isecond);
    }
        break;
    case MASTER_L:
    {
        setting.setValue("masterlevel/hour",mt.ihour);
        setting.setValue("masterlevel/minute",mt.iminute);
        setting.setValue("masterlevel/second",mt.isecond);
    }
        break;
    case GUN_L:
    {
        setting.setValue("gunlevel/hour",mt.ihour);
        setting.setValue("gunlevel/minute",mt.iminute);
        setting.setValue("gunlevel/second",mt.isecond);
    }
        break;
    default:
    {

    }
    }
}

int globalvar::completeNum()
{
    QSettings   setting("MySoft","sudoku");
    return  setting.value("game/complete",0).toInt();
}

void globalvar::setCompleteNum(int n)
{
    QSettings   setting("MySoft","sudoku");
    setting.setValue("game/complete",n);
}

QString globalvar::levelDate(int l)
{
    QString strdate("");
    QSettings   setting("MySoft","sudoku");
    switch(l)
    {
    case NEW_L:
    {
        strdate = setting.value("newlevel/date","").toString();
    }
        break;
    case NORMAL_L:
    {
        strdate = setting.value("normallevel/date","").toString();
    }
        break;
    case MASTER_L:
    {
        strdate = setting.value("masterlevel/date","").toString();
    }
        break;
    case GUN_L:
    {
        strdate = setting.value("gunlevel/date","").toString();
    }
        break;
    default:
    {

    }
    }
    return strdate;
}

void globalvar::setLevelDate(QString strdate, int l)
{
    QSettings   setting("MySoft","sudoku");
    switch(l)
    {
    case NEW_L:
    {
        setting.setValue("newlevel/date",strdate);
    }
        break;
    case NORMAL_L:
    {
        setting.setValue("normallevel/date",strdate);
    }
        break;
    case MASTER_L:
    {
        setting.setValue("masterlevel/date",strdate);
    }
        break;
    case GUN_L:
    {
        setting.setValue("gunlevel/date",strdate);
    }
        break;
    default:
    {

    }
    }
}

bool globalvar::IsVoice()
{
    QSettings   setting("MySoft","sudoku");
    return setting.value("game/voice",false).toBool();
}

void globalvar::SetVoice(bool bvoice)
{
    QSettings   setting("MySoft","sudoku");
    setting.setValue("game/voice",bvoice);
}

bool globalvar::IsShowContinue()
{
    QSettings   setting("MySoft","sudoku");
    return  setting.value("main/sc",false).toBool();
}

void globalvar::SetShowContinue(bool bshow)
{
    QSettings   setting("MySoft","sudoku");
    setting.setValue("main/sc",bshow);
}

void globalvar::restoreChess(NUMTP numtparr[81], MyTime &mt, int &ipromat,
                             int &icurl,int &ierr)
{
    QSettings   setting("MySoft","sudoku");
    for(int i = 0; i < 81; ++i)
    {
        int m = static_cast<int>(i / 9),n = i % 9;
        numtparr[i].ishow = setting.value(QString("chess/sudo%1").arg(i),0).toInt();
        numtparr[i].ianswer = setting.value(QString("chess/sudot%1").arg(i),0).toInt();
        numtparr[i].menolist.clear();
        int imenocount = setting.value(QString("chess/sudo%1menocount").arg(i),0).toInt();
        if(imenocount > 0)
        {
            for(int j = 0; j < imenocount;++j)
            {
                numtparr[i].menolist.append(setting.value(
                                                QString("chess/sudo%1meno").arg(j),1).toInt());
            }
        }
    }

    mt.ihour = setting.value("chess/hour",0).toInt();
    mt.iminute = setting.value("chess/minute",0).toInt();
    mt.isecond = setting.value("chess/second",0).toInt();

    ipromat = setting.value("chess/promat",0).toInt();

    icurl = setting.value("chess/curl",NEW_L).toInt();

    ierr = setting.value("chess/err",0).toInt();
}

void globalvar::storeChess(NUMTP numtparr[81],const MyTime &mt, int ipromat,
                           int icurl,int ierr)
{
    QSettings   setting("MySoft","sudoku");
    for(int i = 0; i < 81; ++i)
    {
        int m = static_cast<int>(i / 9),n = i % 9;
        setting.setValue(QString("chess/sudo%1").arg(i),numtparr[i].ishow);
        setting.setValue(QString("chess/sudot%1").arg(i),numtparr[i].ianswer);
        int imenocount = numtparr[i].menolist.count();
        setting.setValue(QString("chess/sudo%1menocount").arg(i),imenocount);
        if(imenocount > 0)
        {
            for(int j = 0; j < imenocount;++j)
            {
                setting.setValue(QString("chess/sudo%1meno").arg(j),numtparr[i].menolist[j]);
            }
        }
    }

    setting.setValue("chess/hour",mt.ihour);
    setting.setValue("chess/minute",mt.iminute);
    setting.setValue("chess/second",mt.isecond);

    setting.setValue("chess/promat",ipromat);

    setting.setValue("chess/curl",icurl);

    setting.setValue("chess/err",ierr);
}
